<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | relativeTime }}</span>
            <van-button size="mini" type="default" @click="replyComment(item)">回复{{ item.reply_count }}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="msg"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup v-model="show" round position="bottom" :style="{ height: '80%' }">
      <!-- 评论回复 -->
      <comment-reply v-if="show" :comment="comment" @close="show = false" />
    </van-popup>
  </div>
</template>

<script>
import { reqGetArticleComments, reqAddComment } from '@/api/article'
import CommentReply from './CommentReply'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {
    artId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      msg: '', // 发表评论的信息
      show: false, // 控制回复列表的弹层
      comment: {}
    }
  },

  methods: {
    async onLoad() {
      const { data: { data: { results, last_id }}} = await reqGetArticleComments('a', this.artId, this.offset)
      //  展开追加赋值
      this.list.push(...results)
      // 赋值offset
      this.offset = last_id
      // 手动关闭loading
      this.loading = false
      // 加载结束
      if (!results.length) {
        this.finished = true
      }
    },
    // 发表评论
    async addComment() {
      // 调用发表评论的接口
      const { data: { data: { new_obj }}} = await reqAddComment(this.artId, this.msg)
      console.log(new_obj) // 新增的评论信息对象
      this.list.unshift(new_obj)
      // 清空输入框
      this.msg = ''
    },
    // 点击回复按钮显示回复列表的弹层
    replyComment(item) {
      // console.log(item)  当前点击的评论项 存储在data中,父传子传递给子组件
      this.comment = item
      this.show = true
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
